
{% import 'appbuilder/general/lib.html' as lib %}

{% set can_add = "can_add" | is_item_visible(modelview_name) %}
{% set can_show = "can_show" | is_item_visible(modelview_name) %}
{% set can_edit = "can_edit" | is_item_visible(modelview_name) %}
{% set can_delete = "can_delete" | is_item_visible(modelview_name) %}
{% set actions = actions | get_actions_on_list(modelview_name) %}

{% if can_add %}
<span class="list-add-action">
    {% set path = url_for(modelview_name + '.add') %}
    {% set path = path | set_link_filters(filters) %}
    &nbsp;{{ lib.lnk_add(path) }}
</span>
{% endif %}

{% if count > 0 %}

{% block begin_content scoped %}
{% endblock %}

{% block begin_loop_header scoped %}
{% endblock %}

{% block begin_loop_values %}
{% endblock %}

{% block end_content scoped %}
{% endblock %}

<div class="form-actions-container">
    <!-- 添加全局批量操作按钮 -->
    {% if modelview_name == 'Training_Model_ModelView' %}
    <div class="batch-actions-container" style="margin-bottom: 10px;">
        <button type="button" class="btn btn-primary" id="batch-analyze-btn" disabled>
            <i class="fa fa-search"></i> 批量分析依赖
        </button>
        <button type="button" class="btn btn-info" id="batch-view-btn" disabled>
            <i class="fa fa-sitemap"></i> 批量查看依赖
        </button>
    </div>
    {% endif %}
    {{ lib.render_actions(actions, modelview_name) }}
</div>
{{ lib.action_form(actions,modelview_name) }}

<div class="pagination-container pull-right">
    <strong>{{ _('Record Count') }}:</strong> {{ count }}
    {{ lib.render_pagination(page, page_size, count, modelview_name) }}
    {{ lib.render_set_page_size(page, page_size, count, modelview_name) }}
</div>
<script language="javascript">
    var modelActions = new AdminActions();

    {% if modelview_name == 'Training_Model_ModelView' %}
    // 批量操作功能
    $(document).ready(function() {
        // 监听复选框变化
        $('.action_check_all, .action_check').change(function() {
            var checkedBoxes = $('.action_check:checked');
            var batchAnalyzeBtn = $('#batch-analyze-btn');
            var batchViewBtn = $('#batch-view-btn');
            
            if (checkedBoxes.length > 0) {
                batchAnalyzeBtn.prop('disabled', false);
                batchViewBtn.prop('disabled', false);
            } else {
                batchAnalyzeBtn.prop('disabled', true);
                batchViewBtn.prop('disabled', true);
            }
        });
        
        // 批量分析依赖
        $('#batch-analyze-btn').click(function() {
            var selectedIds = [];
            $('.action_check:checked').each(function() {
                selectedIds.push($(this).val());
            });
            
            if (selectedIds.length === 0) {
                alert('请选择至少一个模型');
                return;
            }
            
            if (confirm('确定要批量分析选中模型的依赖关系吗？')) {
                $.ajax({
                    url: '/training_model_modelview/api/batch_analyze_dependencies',
                    method: 'POST',
                    data: {
                        'model_ids': selectedIds.join(',')
                    },
                    dataType: 'json',
                    success: function(response) {
                        if (response.status === 200) {
                            alert(response.message || '批量分析完成');
                        } else {
                            alert(response.message || '批量分析失败');
                        }
                        location.reload();
                    },
                    error: function(xhr, status, error) {
                        console.error('AJAX错误:', xhr.responseText);
                        alert('批量分析失败: ' + (xhr.responseJSON?.message || error));
                    }
                });
            }
        });
        
        // 批量查看依赖
        $('#batch-view-btn').click(function() {
            var selectedIds = [];
            $('.action_check:checked').each(function() {
                selectedIds.push($(this).val());
            });
            
            if (selectedIds.length === 0) {
                alert('请选择至少一个模型');
                return;
            }
            
            var url = '/training_model_modelview/web/api/batch_dependencies/' + selectedIds.join(',');
            window.open(url, '_blank');
        });
    });
    {% endif %}
</script>

{% else %}
<b>{{_("No records found")}}</b>
{% endif %}
